#include <iostream>
#include <vector>
#include <string>

#include <cstring>

#include <unistd.h>
#include <pthread.h>

#define NUM 5

const std::string toHex(pthread_t id)
{
    char hex[60];
    sprintf(hex, "%p", id);
    return hex;
}

struct thread_data
{
private:
public:
    void Init(int number)
    {
        _name = "thread_" + std::to_string(number);
    }

    std::string _id;
    std::string _name;
};

void* threadroutine(void* args)
{
    int test_i = 0;
    thread_data* ptd = static_cast<thread_data*>(args);
    ptd->_id = toHex(pthread_self());
    for (int i = 0; i < 10; ++i)
    {
        std::cout << "pid: " << getpid() << ' ';
        std::cout << "thread name: " << ptd->_name << ' ';
        std::cout << "thread id: " << ptd->_id << std::endl;
        sleep(1);
        std::cout << "test_i: " << test_i << " &test_i: " << &test_i << std::endl;
        ++test_i;
    }
    delete ptd;
    return nullptr;
}

int main()
{
    std::vector<pthread_t> tids;

    for (int i = 0; i < NUM; ++i)
    {
        thread_data* ptd = new thread_data;
        ptd->Init(i);
        pthread_t tid;
        pthread_create(&tid, nullptr, threadroutine, (void*)ptd);
        tids.push_back(tid);
        sleep(1);
    }

    for (int i = 0; i < NUM; ++i)
    {
        pthread_join(tids[i], nullptr);
    }

    return 0;
}